home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Network / Ping 1.1 / Source / Util.c < prev    next >
Text File  |  1992-04-18  |  2KB  |  110 lines

  1. //    Util.c
  2. //    Ping 1.0
  3. //
  4. //    A neat hack © 1992 Jon Wätte (h+@nada.kth.se)
  5. //
  6. //    Freeware - permission granted to study & distribute free of charge,
  7. //    however, you may not sell this source or any derivate thereof, nor
  8. //    may you distribute modified source or a derivate thereof.
  9.  
  10. #include "Util.h"
  11.  
  12.  
  13. #define CHOOSER_NAME -16096
  14.  
  15. //    Clear a block of memory
  16. //    This could be done much more efficiently by unrolling and using long moves
  17. //
  18. void
  19. BlockClear ( void * ptr , long size )
  20. {
  21.     register unsigned char * d = ptr ;
  22.     register long c = size ;
  23.  
  24.     while ( c -- ) {
  25.  
  26.         * ( d ++ ) = 0 ;
  27.     }
  28. }
  29.  
  30.  
  31. //    Report fatal error. Gove the user a chance to try again
  32. //
  33. void
  34. FatalError ( short err , char * file , int line )
  35. {
  36.     Str32 lst ;        //    Line Number string
  37.     Str255 fil ;    //    File Name string
  38.     Str32 er ;        //    Error Code string
  39.     Str255 est ;    //    Error Text string
  40.     Handle h ;
  41.  
  42.     InitCursor ( ) ;
  43.     NumToString ( err , er ) ;
  44.     NumToString ( line , lst ) ;
  45.     fil [ 0 ] = strlen ( file ) ;
  46.     BlockMove ( file , & fil [ 1 ] , fil [ 0 ] ) ;
  47.  
  48.     //    If you want specific errors explained, add 'Estr' resource containing
  49.     //    a string, with the ID of the error number.
  50.     h = GetResource ( 'Estr' , err ) ;
  51.     est [ 0 ] = 0 ;
  52.     if ( h ) {
  53.  
  54.         BlockMove ( * h , est , * * h + 1 ) ;
  55.         ReleaseResource ( h ) ;
  56.     }
  57.     ParamText ( est , er , fil , lst ) ;
  58.     if ( 1 == Alert ( 128 , NULL ) ) {
  59.  
  60.         ExitToShell ( ) ;
  61.     }
  62. }
  63.  
  64.  
  65. //    I don't want to include ANSI just for one lousy function...
  66. //
  67. long
  68. strlen ( char * s )
  69. {
  70.     register long c = 0 ;
  71.     register char * p = s ;
  72.  
  73.     while ( * p ++ ) {
  74.  
  75.         c ++ ;
  76.     }
  77.  
  78.     return c ;
  79. }
  80.  
  81.  
  82. //    Return the Chooser name
  83. //
  84. void
  85. GetUserName ( unsigned char * theName )
  86. {
  87.     Handle h = GetResource ( 'STR ' , CHOOSER_NAME ) ;
  88.     char state ;
  89.  
  90.     if ( h && * h ) {
  91.  
  92.         //    Remember not to dispose or "change" system resources.
  93.  
  94.         state = HGetState ( h ) ;
  95.         HLock ( h ) ;
  96.         BlockMove ( * h , theName , * * h + 1 ) ;
  97.         HSetState ( h , state ) ;
  98.     }
  99. }
  100.  
  101.  
  102. unsigned char *
  103. AddPBuf ( unsigned char * dst , unsigned char * src )
  104. {
  105.     BlockMove ( & src [ 1 ] , & dst [ dst [ 0 ] + 1 ] , src [ 0 ] ) ;
  106.     dst [ 0 ] += src [ 0 ] ;
  107.  
  108.     return dst ;
  109. }
  110.